package com.gede.web.esael.Util;

import net.sf.json.JSONObject;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;



import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DCSUtil {

    public static String sendPost(String serverurl, Map<String, String> param) {

        String result = "";

        List<NameValuePair> list = new ArrayList<NameValuePair>();

        String fullurl = "";

//请求参数

        for (String key : param.keySet()) {

            list.add(new BasicNameValuePair(key, param.get(key)));

        }

        try {

            fullurl = EntityUtils.toString(new UrlEncodedFormEntity(list, Consts.UTF_8));

        } catch (Exception e) {

            result = "下载失败";

            e.printStackTrace();

        }

        HttpPost httpget = new HttpPost(serverurl + "?" + fullurl);

        CloseableHttpClient httpclient = HttpClients.createDefault();

        try (CloseableHttpResponse response = httpclient.execute(httpget)) {

            if (response.getStatusLine().getStatusCode() == 200) {

                HttpEntity resEntity = response.getEntity();

                byte[] josn = EntityUtils.toByteArray(resEntity);

                String requestJson = new String(josn, "UTF-8");
                result=requestJson;
                EntityUtils.consume(resEntity);

            } else {

                result = "下载失败";

            }

        } catch (Exception e) {

            result = "下载失败";

            e.printStackTrace();

        } finally {

            httpget.releaseConnection();

        }

        return result;

    }

    /**
     * 向指定 URL 上传文件POST方法的请求
     *
     * @param url      发送请求的 URL
     * @param filepath 文件路径
     * @param type     转换类型
     * @return 所代表远程资源的响应结果, json数据
     */

    public static String SubmitPost(String url, String filepath, String type) {

       String requestJson = "";

        HttpClient httpclient = new DefaultHttpClient();

        try {

            HttpPost httppost = new HttpPost(url);

            FileBody file = new FileBody(new File(filepath));

            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));

            reqEntity.addPart("file", file); // file为请求后台的File upload;属性

            reqEntity.addPart("convertType", new StringBody(type, Charset.forName("UTF-8")));

            httppost.setEntity(reqEntity);

            HttpResponse response = httpclient.execute(httppost);

            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode == HttpStatus.SC_OK) {

                HttpEntity resEntity = response.getEntity();

                byte[] josn = EntityUtils.toByteArray(resEntity);

                requestJson = new String(josn, "UTF-8");

                EntityUtils.consume(resEntity);


            }

        } catch (ParseException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

// requestJson = e.toString();

        } catch (IOException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

// requestJson = e.toString();

        } finally {

            try {

                httpclient.getConnectionManager().shutdown();

            } catch (Exception ignore) {

            }

        }

        return requestJson;

    }

    public static void main(String[] args) {

// TODO Auto-generated method stub

// 文件上传转换

//        String convertByFile = SubmitPost("http://192.168.8.23:8080/dcs.web/upload", "H:/doctest.docx", "29");
//
//        System.out.println(convertByFile);
// 网络地址转换
        Map<String, String> map = new HashMap<String, String>();
        map.put("downloadUrl", "http://192.168.8.23:8080/2020/09/25/MjAwOTI1MDE0MDUzMDcx.ofd");
        map.put("convertType", "1");
        String convertByUrl = sendPost("http://192.168.8.23:8080/dcs.web/onlinefile", map);
        System.out.println("convertByUrl="+convertByUrl);
        JSONObject jsStr = JSONObject.fromObject(convertByUrl);
        List<String> s = (List) jsStr.get("data");
        System.out.println(s.get(0));

    }

}
